package com.htn.gallery.web.control;

import java.util.ArrayList;
import java.util.List;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.htn.gallery.web.model.Album;
import com.htn.gallery.web.model.Photo;
import com.htn.gallery.web.utils.PicasaClient;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ViewPhotosAction extends ActionSupport {

	private List<Album> albums = new ArrayList<Album>();

	public List<Album> getAlbums() {
		return albums;
	}
	
	@Override
	public String execute() {
		
		try {
			PicasawebService myService = new PicasawebService("exampleCo-exampleApp-1");
			myService.setUserCredentials("donghaiit@gmail.com", "Ocean1980");			
			PicasaClient client = new PicasaClient(myService);
			
			for (AlbumEntry myAlbum : client.getAlbums("donghaiit")) {
				
				Album al = new Album();
				String id = myAlbum.getId();
				int length = id.indexOf("albumid/") + 8;
				al.setId(id.substring(length));
				al.setTitle(myAlbum.getTitle().getPlainText());
				al.setName(myAlbum.getName());
				al.setDescription(myAlbum.getDescription().getPlainText());
				al.setThumbnail(myAlbum.getMediaThumbnails().get(0).getUrl());
			    List<Photo> photos = new ArrayList<Photo>();
				for (PhotoEntry photo : client.getPhotos(myAlbum)) {
					System.out.println(photo.getTitle().getPlainText());
					photos.add(new Photo(photo.getTitle().getPlainText(),
							photo.getDescription().getPlainText(),
							photo.getMediaThumbnails().get(0).getUrl(),
							photo.getMediaEditLink().getHref()));
				}
				al.setPhotos(photos);
				albums.add(al);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return INPUT;
		}
		return SUCCESS;
	}
}
